/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

/*
 * Created on 21/08/2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package br.ufmg.dees.insane.xyplot.xyplot_view.dialog;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.xyplot.xyplot_view.InterfaceXYPlot;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotView;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotViewState;

/**
 * @author  Samuel Silva Penna  TODO To change the template for this generated type comment go to  Window - Preferences - Java - Code Style - Code Templates
 */
public class TextBoxSettingsDialog extends SimpleDialog{
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InterfaceXYPlot parent;
    private XYPlotViewState state;
	
	private JPanel colorsTextPane = new JPanel();
    private JTextField textField = new JTextField();
    private JTextField textColorField = new JTextField();
    private JComboBox textFontCombo = new JComboBox();
    private JComboBox textStyleCombo = new JComboBox(); 
    private DecimalField textSizeDecimalField;
    private DecimalField positionXField;
    private DecimalField positionYField;
    private JButton chooseTextColor = new JButton("Choose");
	private java.awt.geom.Rectangle2D.Double limits = new java.awt.geom.Rectangle2D.Double();
    private NumberFormat numberFormat;
    private String[]textFontStr;
    private String[]textStyleStr;
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.Interface
     */
    public TextBoxSettingsDialog(InterfaceXYPlot parent, ViewState s) {
        this.parent = parent;
        state = (XYPlotViewState)s;
		
        setUpFormat();
		
        //Sets up the main panel
        JPanel mainPane = new JPanel();
        mainPane.setLayout(new GridLayout(1,4,2,2));          
        mainPane.setPreferredSize(new Dimension(300,250));
        mainPane.setMaximumSize(new Dimension(300,225));
        mainPane.setLayout(new BoxLayout(mainPane,BoxLayout.Y_AXIS));      
        
        //set up the titlePane
        JPanel textPane = new JPanel();
        textPane.setLayout(new GridLayout(4,2,2,2));          
        textPane.setPreferredSize(new Dimension(300,150));
        textPane.setMaximumSize(new Dimension(300,150));
        textPane.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createTitledBorder("Text Box"),
        		BorderFactory.createEmptyBorder(2,2,2,2)));
        // title field 
        textPane.add(new JLabel("Text:"));
        textField.setMaximumSize(new Dimension(75,50));
        textField.setPreferredSize(new Dimension(75,50));
        textField.setMinimumSize(new Dimension(75,50));
        textField.setSize(new Dimension(75,50));  
        textField.setText(state.getTextBoxState().getText());
        textPane.add(textField);
       
        // title font
        textFontStr = new String[5];
        textFontStr[0] = "Arial";
        textFontStr[1] = "Monospaced";
        textFontStr[2] = "SansSerif";
        textFontStr[3] = "Time"; 
        textFontStr[4] = "NONE"; 
        textFontCombo = new JComboBox(textFontStr);
        textPane.add(new JLabel("Text font:"));
        textFontCombo.setMaximumSize(new Dimension(75,26));
        textFontCombo.setPreferredSize(new Dimension(75,26));
        textFontCombo.setMinimumSize(new Dimension(75,26));
        textFontCombo.setSize(new Dimension(75,26));  
        textFontCombo.setSelectedItem(state.getTextBoxState().getTextFont());
        textPane.add(textFontCombo); 
        // title font size
        textPane.add(new JLabel("Size"));
        textSizeDecimalField = new DecimalField(state.getTextBoxState().getTextSize(),10,numberFormat);
        textSizeDecimalField.setMaximumSize(new Dimension(75,26));
        textSizeDecimalField.setPreferredSize(new Dimension(75,26));
        textSizeDecimalField.setMinimumSize(new Dimension(75,26));
        textSizeDecimalField.setSize(new Dimension(75,26));  
        textSizeDecimalField.setEditable(true);
        textPane.add(textSizeDecimalField);
       // title font Style
        textStyleStr = new String[3];
        textStyleStr[0] = "Plain";
        textStyleStr[1] = "Bold";
        textStyleStr[2] = "Italic";
        textStyleCombo = new JComboBox(textStyleStr);
        textPane.add(new JLabel("Font Style:"));
        textStyleCombo.setMaximumSize(new Dimension(75,26));
        textStyleCombo.setPreferredSize(new Dimension(75,26));
        textStyleCombo.setMinimumSize(new Dimension(75,26));
        textStyleCombo.setSize(new Dimension(75,26));  
        textStyleCombo.setSelectedIndex(state.getTextBoxState().getTextStyle());
        textPane.add(textStyleCombo); 
        
//      Setting subpanel colors Title panel 
        colorsTextPane = new JPanel();
        colorsTextPane.setLayout(new BoxLayout(colorsTextPane, BoxLayout.Y_AXIS));
        colorsTextPane.setMaximumSize(new Dimension(300,45));
        colorsTextPane.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createTitledBorder(""),
        		BorderFactory.createEmptyBorder(2,2,2,2)));
        colorsTextPane.setLayout(new GridLayout(1,3,3,3));
        colorsTextPane.add(new JLabel("Text Color"));
        textColorField = new JTextField();
        textColorField.setEditable(false);
        textColorField.setBackground(state.getTextBoxState().getTextColor());
        colorsTextPane.add(textColorField);
        chooseTextColor = new JButton("Choose");
        colorsTextPane.add(chooseTextColor);
        chooseTextColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                   textColorActionPerformed(e);
                }
            } );
        
        JPanel positionPane = new JPanel();
        positionPane.setLayout(new GridLayout(1,2,2,2));          
        positionPane.setMaximumSize(new Dimension(350,60));
        positionPane.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createTitledBorder("Position"),
        		BorderFactory.createEmptyBorder(2,2,2,2)));  
        
        // title position X  
        JPanel positionXPanel = new JPanel();
        positionXPanel.setLayout(new GridLayout(1,2,2,2));  
        positionXPanel.setMaximumSize(new Dimension(75,26));
        positionXPanel.setPreferredSize(new Dimension(75,26));
        positionXPanel.setMinimumSize(new Dimension(75,26));
        positionXPanel.setSize(new Dimension(75,26));  
        positionXPanel.add(new JLabel("Position X"));
        positionXField = new DecimalField(state.getTextBoxState().getXPosition(),10,numberFormat);
        positionXField.setMaximumSize(new Dimension(75,26));
        positionXField.setPreferredSize(new Dimension(75,26));
        positionXField.setMinimumSize(new Dimension(75,26));
        positionXField.setSize(new Dimension(75,26));  
        positionXField.setValue(state.getTextBoxState().getXPosition());
        positionXPanel.add(positionXField);
        positionPane.add(new JLabel("Position X"));
        positionPane.add(positionXField);
        // title position Y
        JPanel positionYPanel = new JPanel();
        positionYPanel.setLayout(new GridLayout(1,2,2,2));  
        positionYPanel.setMaximumSize(new Dimension(75,26));
        positionYPanel.setPreferredSize(new Dimension(75,26));
        positionYPanel.setMinimumSize(new Dimension(75,26));
        positionYPanel.setSize(new Dimension(75,26));  
        positionYPanel.add(new JLabel("Position Y"));
        positionYField = new DecimalField(state.getTextBoxState().getYPosition(),10,numberFormat);
        positionYField.setMaximumSize(new Dimension(75,26));
        positionYField.setPreferredSize(new Dimension(75,26));
        positionYField.setMinimumSize(new Dimension(75,26));
        positionYField.setValue(state.getTextBoxState().getYPosition());
        positionYPanel.add(positionYField);
        positionPane.add(new JLabel("Position Y"));
        positionPane.add(positionYField);
        
        mainPane.add(textPane);
        mainPane.add(positionPane);
        mainPane.add(colorsTextPane);
        
        
        setPane(mainPane);
    }
	
//*****************************************************************************
    
    public int showDialog() {
        return super.showDialog(parent, "TextBox Settings");
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
            
			state.setLimits(limits);
        	state.getTextBoxState().setText((String)this.textField.getText());
        	state.getTextBoxState().setTextFont((String)this.textFontCombo.getSelectedItem());
        	state.getTextBoxState().setTextSize((int)this.textSizeDecimalField.getValue());
        	state.getTextBoxState().setTextStyle((byte)this.textStyleCombo.getSelectedIndex());
        	state.getTextBoxState().setXPosition((double)this.positionXField.getValue());
			state.getTextBoxState().setYPosition((double)this.positionYField.getValue());
			state.getTextBoxState().setTextColor(this.textColorField.getBackground());
			
			//parent.setModel(((XYPlotController)parent.getView().getController()).getModel());
            this.setVisible(false);
            ((XYPlotView)parent.getView()).getDrawingArea().repaint();
		//	LastZoomCommand cmd = new LastZoomCommand(parent);
		//	cmd.execute();
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
//*****************************************************************************
	
    private void textColorActionPerformed(ActionEvent e) {
        textColorField.setBackground(JColorChooser.showDialog(this, "Choose Text Color", textColorField.getBackground()));
    }
    
//*****************************************************************************
	
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(3);
    }
	
//*****************************************************************************
}


